<?php
class ReportsManager extends BaseManager implements iManager {
	
	public function query(){
		
	}
	
	public function add(){
		
	}
	
	public function update(){
		
	}
	
	public function delete(){
		
	}
	
	public function getSales($filerDate){
		
		$ret = true;
		
		$query = "SELECT 
					sdt.product_id AS `product_id`, 
					pt.product_name AS `product_name`,
					SUM(sdt.product_price) AS `product_price`,
					SUM(sdt.product_count) AS `product_count`, 
					pt.current_count AS `stock`
					FROM 
					`sales_detail_tbl` AS sdt
					INNER JOIN `sales_tbl` AS st
					ON sdt.sales_id = st.sales_id
					INNER JOIN `product_tbl` AS pt
					ON sdt.product_id = pt.product_id
					WHERE st.date_sold >= '" . $filerDate ." 00:00:00' AND st.date_sold <= '" . $filerDate ." 59:59:59'
					GROUP BY sdt.product_id;";
		
		$this->db->query($query);
		
		$this->db->execute();
		
		//$result = $this->db->getResultAssoc();
		
		$listOfSales = array();
		
		while ($result = $this->db->getResultAssoc()) {
			
			$salesReport = new SalesReport_Domain();
			
			$salesReport->setProductId($result['product_id']);
			$salesReport->setProductName($result['product_name']);
			$salesReport->setTotalAmountSold($result['product_price']);
			$salesReport->setTotalNoSold($result['product_count']);
			$salesReport->setRemainingStock($result['stock']);
			
			$listOfSales[] = $salesReport;
		}

		$this->result = $listOfSales;		
	}
	
	public function getInventory(){
		$ret = true;
		
		$query = "SELECT product_id, product_name, current_count FROM `product_tbl`;";
		
		$this->db->query($query);
		
		$this->db->execute();
		
		//$result = $this->db->getResultAssoc();
		
		$listOfInventory = array();
		
		while ($result = $this->db->getResultAssoc()) {
				
			$product = new Product_Domain();
				
			$product->setProductId($result['product_id']);
			$product->setProductName($result['product_name']);
			$product->setProductQty($result['current_count']);
				
			$listOfInventory[] = $product;
		}
		
		$this->result = $listOfInventory;
	}
	
	public function getComplaints($filerDate){
		$ret = true;
		
		$query = "SELECT 
					ct.sales_id,
					pt.product_name,
					ct.product_count,
					ct.product_price,
					ct.employee_id,
					ct.processed_by,
					ct.complaint_date,
					ct.reason
					FROM `complaints_tbl` AS ct
					INNER JOIN `product_tbl` AS pt
					ON ct.prouct_id = pt.product_id
					WHERE ct.complaint_date >= '" . $filerDate ." 00:00:00' AND  ct.complaint_date <= '" . $filerDate ." 59:59:59';";
		
		$this->db->query($query);
		
		$this->db->execute();
		
		//$result = $this->db->getResultAssoc();
		
		$listOfComplaints = array();
		
		while ($result = $this->db->getResultAssoc()) {
				
			$complaint = new ComplaintReport_Domain();
				
			$complaint->setSalesId($result['sales_id']);
			$complaint->setProductName($result['product_name']);
			$complaint->setProductCount($result['product_count']);
			$complaint->setProductPrice($result['product_price']);
			$complaint->setAccountable($result['employee_id']);
			$complaint->setProcessedBy($result['processed_by']);
			$complaint->setComplaintDate($result['complaint_date']);
			$complaint->setReason($result['reason']);
				
			$listOfComplaints[] = $complaint;
		}
		
		$this->result = $listOfComplaints;
	}
	
}